package com.filldream.fastboot.common.okutil.stock.commonComment;

import cn.hutool.core.date.DateUtil;
import com.filldream.fastboot.common.okutil.cls.ClsUtil;
import com.filldream.fastboot.common.okutil.cls.dto.ClsAnchorDTO;
import com.filldream.fastboot.common.okutil.cls.dto.ClsNewsSimpleItem;
import com.filldream.fastboot.common.okutil.stock.cf.comment.CfCommentUtil;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfComment;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfCommentPostUser;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfCommentReply;
import com.filldream.fastboot.common.okutil.stock.ths.comment.ThsCommentUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class CommonCommentUtil {

    final static String defaultCodes = "sh513130,sz159338,sh516640,sh512670,sh512170";

    public static List<CfComment> getComments(String codes) {
        if( StringUtils.isBlank(codes) ){
            codes = defaultCodes;
        }
        List<CfComment> result = new ArrayList<>();
        final String[] codeList = codes.split(",");
        for (String code : codeList) {
            List<CfComment> cfComments = CfCommentUtil.getComments(code);
            for (CfComment item : cfComments) {
                item.setPost_content( item.getShowContent() );
            }
            result.addAll(cfComments);
            //同花顺
            result.addAll(ThsCommentUtil.getComments2(code));
        }
        //电报
        result.addAll( ClsUtil.getTel() );
        //财联社头条
        List<ClsNewsSimpleItem> clsNewsList = ClsUtil.getClsNewsList();
        result.addAll(clsNewsList.stream().map(ClsNewsSimpleItem::toCfComment).collect(Collectors.toList()));
        //财联社版块变动
        List<ClsAnchorDTO> clsAnchorList = ClsUtil.getClsAnchor(DateUtil.formatDate(new Date()));
        if(ObjectUtils.isNotEmpty(clsAnchorList)){
            ClsAnchorDTO firstItem = clsAnchorList.get(0);
            CfComment anchorComment = new CfComment();
            anchorComment.setChannelType(5);
            anchorComment.setPost_id( firstItem.getArticleId() );
            anchorComment.setTheme( "" );
            anchorComment.setPost_title("");
            anchorComment.setPost_content( "财联社板块变动,具体看评论的回复列表,来自FastUtil" );
            anchorComment.setPost_pic_url( new ArrayList<>() );
            anchorComment.setPost_publish_time( firstItem.getCTime() );
            anchorComment.setStockName( anchorComment.getChannelName() + "变动" );
            CfCommentPostUser cfCommentPostUser = new CfCommentPostUser();
            cfCommentPostUser.setUser_age("财联社");
            cfCommentPostUser.setUser_nickname( anchorComment.getChannelName() );
            anchorComment.setPost_user( cfCommentPostUser);
            anchorComment.setReply_list(new ArrayList<>());
            ArrayList<CfCommentReply> anchorReplyList = new ArrayList<>();
            for (ClsAnchorDTO item : clsAnchorList) {
                CfCommentPostUser anchorReplyUser = new CfCommentPostUser();
                anchorReplyUser.setUser_nickname(anchorComment.getChannelName());
                CfCommentReply reply = new CfCommentReply();
                reply.setReply_text("板块:" + item.getSymbolName() + (item.getIsUp()  ? " ↑ " : " ↓ " ));
                reply.setReply_time(item.getCTime());
                reply.setReply_user(anchorReplyUser);
                reply.setReply_id( item.getArticleId() );
                anchorReplyList.add(reply);
            }
            anchorComment.setReply_list(anchorReplyList);
            result.add(anchorComment);
        }

        //统一处理时间，今日的直接显示时间，非今日的显示全部
        for (CfComment item : result) {
            final String commentType = item.getChannelName();
            int compare = DateUtil.compare(DateUtil.parseDate(item.getPost_publish_time()), DateUtil.beginOfDay(new Date()));
            String sendTime =  item.getPost_publish_time().substring(compare >=0 ? 11 : 5);
            item.setPost_publish_time(sendTime);
            //统一处理回复时间
            item.getReply_list().forEach(t -> {
                if(StringUtils.isNotBlank(t.getReply_time())){
                    int replyCompare = DateUtil.compare(DateUtil.parseDate(t.getReply_time()), DateUtil.beginOfDay(new Date()));
                    t.setReply_time(t.getReply_time().substring(replyCompare >=0 ? 11 : 5));
                }else{
                    t.setReply_time("");
                }
                if( StringUtils.isBlank(t.getReply_user().getUser_age()) ){
                    CfCommentPostUser replyUser = t.getReply_user();
                    replyUser.setUser_age("-年");
                }
            });
            if( StringUtils.isBlank(item.getPost_user().getUser_age()) ){
                CfCommentPostUser postUser = item.getPost_user();
                postUser.setUser_age(commentType);
            }
        }
        return result;
    }

}
